package com.szosen.jkzj.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.text.method.ReplacementTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.szosen.application.MyApplication;
import com.szosen.jkzj.R;
import com.szosen.json.Archive;
import com.szosen.json.ArchiveDetail;
import com.szosen.json.Data_getArchiveDetail;
import com.szosen.json.GetArchiveDetail;
import com.szosen.json.PastHistorylist;
import com.szosen.json.database.ArchiveBean;
import com.szosen.json.database.JSONDBService;
import com.szosen.json.pasthistory;
import com.szosen.util.BackGroundThread;
import com.szosen.util.BackGroundThread.BackGroundTask;
import com.szosen.util.IdCardCheckTool;
import com.szosen.util.JKDAContantsTest;
import com.szosen.util.JibingContants;
import com.szosen.util.JktjBitmapUtil;
import com.szosen.util.NationUtil;
import com.szosen.util.SingleChoiceEditText;
import com.szosen.util.TimeHelper;
import com.szosen.util.ToastTool;
import com.szosen.util.jwsColmunAdapter;
import com.szosen.web.WebService;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

public class JKDAActivity extends BaseActivity {
    private EditText nameEditText;
    private EditText userIdEditText;
    private EditText genderEditText;
    private EditText birthEditText;
    private EditText idNumberEditText;
    private EditText companyEditText;
    private EditText selfPhoneEditText;
    private EditText contactNameEditText;
    private EditText contactPhoneEditText;
    private EditText permanentTypeEditText;
    private EditText nationEditText;
    private EditText bloodTypeEditText;
    private EditText rHTypeEditText;
    private EditText maritalStatusEditText;
    private EditText educationDegreeEditText;
    private EditText jobsEditText;
    private EditText paymentTypeEditText;
    private EditText allergicHistoryEditText;
    private EditText exposeHistoryEditText;
    private EditText illnessEditText;
    private EditText operationEditText;
    private EditText traumaEditText;
    private EditText transfusionEditText;
    private EditText fatherHistoryEditText;
    private EditText motherHistoryEditText;
    private EditText brotherSisterHistoryEditText;
    private EditText childernHistoryEditText;
    private EditText genopathyEditText;
    private EditText deformityStateEditText;
    private EditText airExhaustEditText;
    private EditText fuleTypeEditText;
    private EditText waterTypeEditText;
    private EditText toiletTypeEditText;
    private EditText animalHomeEditText;
    private SQLiteDatabase db;
    private Cursor cursor;
    private boolean isExist;
    private static final String TABLE_NAME = "BASICINFOR";
    private Button btn_save;
    // 返回activity数据的resultcode
    public static final int RESULTCODE_JIBINGDATA = 66;
    public static final int REQUESTODE_JIBINGDATA = 55;
    public static final String RESULTDATA_JIBINGDATA = "resultData";
    public static final String JWSLB = "JWSLB";
    public static final String JBMC = "JBMC";
    public static final String GCFF = "GCFF";
    public static final String ZGQK = "ZGQK";
    /*
     * 个人信息——personInfo
     */
    private EditText grxm01;// 姓名
    private AutoCompleteTextView wgdz;// 现住址
    private EditText dabh01;// 健康档案编号
    private RadioGroup grxb01;// 性别
    private RadioButton gexi_xb01;// 男
    private RadioButton gexi_xb02;// 女
    private EditText csrq01;// 出生日期:
    private EditText sfzh01;// 身份证号:
    private EditText gzdw01;// 工作单位:
    private SingleChoiceEditText grmz01;// 民族:
    private EditText grdh01;// 本人电话:
    private EditText lxr001;// 联系人姓名:
    private EditText lxdh01;// 联系人电话:
    private EditText grbm;// 个人编码
    private EditText gxjg;// 管辖机构
    private EditText zrys;// 责任医生
    private RadioGroup xzjg01;// 常住类型:
    private RadioButton xzjg_hj;// 户籍
    private RadioButton xzjg_fhj;// 非户籍
    private RadioButton nyhj_s;// 户籍
    private RadioButton nyhj_f;// 非户籍
    /*
     * 一般情况——person_yiban
     */
    private RadioGroup grxx01;// 血型：
    private RadioButton grxx_A;
    private RadioButton grxx_B;
    private RadioButton grxx_O;
    private RadioButton grxx_AB;
    private RadioButton grxx_BX;
    private RadioGroup rhyx01;// RH阴性：
    private RadioButton rhyx_N;
    private RadioButton rhyx_Y;
    private RadioButton rhyx_BX;
    private RadioGroup hyzk01;// 婚姻状况：
    private RadioButton hyzk_WH;
    private RadioButton hyzk_YH;
    private RadioButton hyzk_CH;
    private RadioButton hyzk_ZH;
    private RadioButton hyzk_FH;
    private RadioButton hyzk_SO;
    private RadioButton hyzk_LH;
    private RadioButton hyzk_QT;
    private RadioGroup whcd01;// 文化程度:
    private RadioButton whcd_YJS;
    private RadioButton whcd_DXBK;
    private RadioButton whcd_DX;
    private RadioButton whcd_ZZ;
    private RadioButton whcd_JGXX;
    private RadioButton whcd_GZ;
    private RadioButton whcd_CZ;
    private RadioButton whcd_XX;
    private RadioButton whcd_WM;
    private RadioButton whcd_BX;

    private RadioGroup grzy01;// 职业：
    private RadioButton grzy_JG;
    private RadioButton grzy_JS;
    private RadioButton grzy_BS;
    private RadioButton grzy_SY;
    private RadioButton grzy_LL;
    private RadioButton grzy_SC;
    private RadioButton grzy_JR;
    private RadioButton grzy_BB;
    private RadioButton grzy_WZY;
    private RadioGroup yblx01;// 医疗费支付方式：
    private RadioButton yblx_ZG;
    private RadioButton yblx_JM;
    private RadioButton yblx_SY;
    private RadioButton yblx_LC;
    private RadioButton yblx_PK;
    private RadioButton yblx_GF;
    private RadioButton yblx_ZF;
    private RadioButton yblx_QT;
    private RadioButton sfhz_hj;// 是否户主
    private RadioButton sfhz_fhj;

    private RadioGroup ycbs01;// 遗传病史
    private RadioButton ycbs_N;
    private RadioButton ycbs_Y;
    private RadioGroup cjqk01;// 残疾情况：
    private CheckBox cjqk_N;
    private CheckBox cjqk_SLCJ;
    private CheckBox cjqk_TLCJ;
    private CheckBox cjqk_YYCJ;
    private CheckBox cjqk_ZTCJ;
    private CheckBox cjqk_ZLCJ;
    private CheckBox cjqk_JSCJ;
    private CheckBox cjqk_QTCJ;
    private EditText cjqk_et;
    private EditText qtzf;// 其他付费方式（补充）
    private EditText ycbs02;// 疾病名称
    /**
     * 生活情况——personShenghuo
     */
    private RadioGroup cfss01;// 厨房排风设施代码
    private RadioButton cfss01_W;// 无
    private RadioButton cfss01_YYJ;// 油烟机
    private RadioButton cfss01_HQS;// 换气扇
    private RadioButton cfss01_YC;// 烟囱
    private RadioButton cfss01_QT;// 烟囱
    private RadioGroup rllx01;// 燃料类型
    private RadioButton rllx01_YHQ;// 液化气
    private RadioButton rllx01_M;// 煤
    private RadioButton rllx01_TRQ;// 天然气
    private RadioButton rllx01_ZQ;// 炤气
    private RadioButton rllx01_CH;// 柴火
    private RadioButton rllx01_QT;// 其他
    private RadioGroup ysqk01;// 饮水
    private RadioButton ysqk01_ZLS;// 自来水
    private RadioButton ysqk01_JJH;// 经净化过滤的水
    private RadioButton ysqk01_JS;// 井水
    private RadioButton ysqk01_HHS;// 河湖水
    private RadioButton ysqk01_TS;// 塘水
    private RadioButton ysqk01_QT;// 其他
    private RadioGroup csqk01;// 厕所
    private RadioButton csqk01_WSCS;// 卫生厕所
    private RadioButton csqk01_FCS;// 一格或二格粪池式
    private RadioButton csqk01_MT;// 马桶
    private RadioButton csqk01_FK;// 露天粪坑
    private RadioButton csqk01_PC;// 简易棚厕
    private RadioButton csqk01_QT;// 其他厕所
    private RadioGroup qcqk01;// 禽畜栏
    private RadioButton qcqk01_DS;// 单设
    private RadioButton qcqk01_SN;// 室内
    private RadioButton qcqk01_SW;// 室外
    private RadioButton qcqk01_W;// 无
    private RadioGroup jdfs01;// 建档方式
    private RadioButton jdfs01_ZL;// 诊疗服务
    private RadioButton jdfs01_GW;// 公卫服务
    private RadioButton jdfs01_YTJ;// 一体机新建
    private RadioButton jdfs01_QT;// 其他服务
    private RadioButton jjly_SHJJ;// 经济来源
    private RadioButton jjly_GZSR;// 其他服务
    private RadioButton jjly_QT;// 其他服务
    private EditText czsj01;// 操作时间
    private Context context;
    private String time = "";
    ListView jws_lv_jws;
    List<String[]> jwsListStr;
    jwsColmunAdapter adapter;
    List<PastHistorylist> jwsList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jkda_zhuhai);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        super.setTitleContent("【健康档案】");
        context = this;
        jwsListStr = new ArrayList<String[]>();
        jwsList = new ArrayList<PastHistorylist>();
        findView();
        initPersonInfoView();
        String time = getIntent().getStringExtra("time");
        if (!TextUtils.isEmpty(time)) {
            this.time = time;
        } else {
            this.time = TimeHelper.getMainTimeKey();
        }
        flag = getIntent().getStringExtra("flag");
        fillTable();
    }

    public void finishActivity(View view) {
        onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULTCODE_JIBINGDATA) {
            String json = data.getStringExtra(RESULTDATA_JIBINGDATA);
            System.out.println("获取json" + json);
            Gson gson = new Gson();
            PastHistorylist phl = gson.fromJson(json, PastHistorylist.class);
            // 增加到list中。list是疾病史listview的数据。
            String jwslb = data.getStringExtra(JWSLB);
            String jbmc = data.getStringExtra(JBMC);
            String gcff = data.getStringExtra(GCFF);
            String zgqk = data.getStringExtra(ZGQK);
            String[] item = new String[]{jwslb, jbmc, gcff, zgqk};
            jwsListStr.add(item);
            System.out.println(phl == null);
            if (phl != null) {
                jwsList.add(phl);
            }
            adapter.notifyDataSetChanged();
        }
    }

    public void printTable(View view) {
        new BackGroundThread(new BackGroundTask() {
            @Override
            public void process() {
                Archive archive = getJsonUpload();
                archive.setPastHistorylist(jwsList);
                JktjBitmapUtil.createJkdaBitmap(context, archive, mGetArchiveDetail);
            }
        }).start();
    }

    @Override
    @SuppressLint("ResourceAsColor")
    protected void onResume() {
        super.onResume();
    }

    public void addJWSData(View view) {
        Intent intent = new Intent(JKDAActivity.this, JiBingActivity.class);
        startActivityForResult(intent, REQUESTODE_JIBINGDATA);
    }

    private void InitPersonYibanView(Activity view1) {
        // 血型
        grxx01 = (RadioGroup) view1.findViewById(R.id.grxx01);
        grxx_A = (RadioButton) view1.findViewById(R.id.grxx_A);
        grxx_B = (RadioButton) view1.findViewById(R.id.grxx_B);
        grxx_O = (RadioButton) view1.findViewById(R.id.grxx_O);
        grxx_AB = (RadioButton) view1.findViewById(R.id.grxx_AB);
        grxx_BX = (RadioButton) view1.findViewById(R.id.grxx_BX);
        // RH阴性
        rhyx01 = (RadioGroup) view1.findViewById(R.id.rhyx01);
        rhyx_N = (RadioButton) view1.findViewById(R.id.rhyx_N);
        rhyx_Y = (RadioButton) view1.findViewById(R.id.rhyx_Y);
        rhyx_BX = (RadioButton) view1.findViewById(R.id.rhyx_BX);
        // 婚姻状况
        hyzk01 = (RadioGroup) view1.findViewById(R.id.hyzk01);
        hyzk_WH = (RadioButton) view1.findViewById(R.id.hyzk_WH);
        hyzk_YH = (RadioButton) view1.findViewById(R.id.hyzk_YH);
        hyzk_SO = (RadioButton) view1.findViewById(R.id.hyzk_SO);
        hyzk_LH = (RadioButton) view1.findViewById(R.id.hyzk_LH);
        hyzk_QT = (RadioButton) view1.findViewById(R.id.hyzk_QT);
        hyzk_WH = (RadioButton) view1.findViewById(R.id.hyzk_WH);
        hyzk_YH = (RadioButton) view1.findViewById(R.id.hyzk_YH);
        hyzk_CH = (RadioButton) view1.findViewById(R.id.hyzk_CH);
        hyzk_ZH = (RadioButton) view1.findViewById(R.id.hyzk_ZH);
        hyzk_FH = (RadioButton) view1.findViewById(R.id.hyzk_FH);
        hyzk_SO = (RadioButton) view1.findViewById(R.id.hyzk_SO);
        hyzk_LH = (RadioButton) view1.findViewById(R.id.hyzk_LH);
        hyzk_QT = (RadioButton) view1.findViewById(R.id.hyzk_QT);

        // 文化程度
        whcd01 = (RadioGroup) view1.findViewById(R.id.whcd01);
        whcd_WM = (RadioButton) view1.findViewById(R.id.whcd_WM);
        whcd_XX = (RadioButton) view1.findViewById(R.id.whcd_XX);
        whcd_CZ = (RadioButton) view1.findViewById(R.id.whcd_CZ);
        whcd_GZ = (RadioButton) view1.findViewById(R.id.whcd_GZ);
        whcd_DX = (RadioButton) view1.findViewById(R.id.whcd_DX);
        whcd_BX = (RadioButton) view1.findViewById(R.id.whcd_BX);

        whcd_YJS = (RadioButton) view1.findViewById(R.id.whcd_YJS);
        whcd_DXBK = (RadioButton) view1.findViewById(R.id.whcd_DXBK);
        whcd_DX = (RadioButton) view1.findViewById(R.id.whcd_DX);
        whcd_ZZ = (RadioButton) view1.findViewById(R.id.whcd_ZZ);
        whcd_JGXX = (RadioButton) view1.findViewById(R.id.whcd_JGXX);
        whcd_GZ = (RadioButton) view1.findViewById(R.id.whcd_GZ);
        whcd_CZ = (RadioButton) view1.findViewById(R.id.whcd_CZ);
        whcd_XX = (RadioButton) view1.findViewById(R.id.whcd_XX);
        whcd_WM = (RadioButton) view1.findViewById(R.id.whcd_WM);
        whcd_BX = (RadioButton) view1.findViewById(R.id.whcd_BX);
        // 职业
        grzy01 = (RadioGroup) view1.findViewById(R.id.grzy01);
        grzy_JG = (RadioButton) view1.findViewById(R.id.grzy_JG);
        grzy_JS = (RadioButton) view1.findViewById(R.id.grzy_JS);
        grzy_BS = (RadioButton) view1.findViewById(R.id.grzy_BS);
        grzy_SY = (RadioButton) view1.findViewById(R.id.grzy_SY);
        grzy_LL = (RadioButton) view1.findViewById(R.id.grzy_LL);
        grzy_SC = (RadioButton) view1.findViewById(R.id.grzy_SC);
        grzy_JR = (RadioButton) view1.findViewById(R.id.grzy_JR);
        grzy_BB = (RadioButton) view1.findViewById(R.id.grzy_BB);
        grzy_WZY = (RadioButton) view1.findViewById(R.id.grzy_WZY);
        // 医疗付费方式
        yblx01 = (RadioGroup) view1.findViewById(R.id.yblx01);
        yblx_ZG = (RadioButton) view1.findViewById(R.id.yblx_ZG);
        yblx_JM = (RadioButton) view1.findViewById(R.id.yblx_JM);
        yblx_SY = (RadioButton) view1.findViewById(R.id.yblx_SY);
        yblx_LC = (RadioButton) view1.findViewById(R.id.yblx_LC);
        yblx_PK = (RadioButton) view1.findViewById(R.id.yblx_PK);
        yblx_GF = (RadioButton) view1.findViewById(R.id.yblx_GF);
        yblx_ZF = (RadioButton) view1.findViewById(R.id.yblx_ZF);
        yblx_QT = (RadioButton) view1.findViewById(R.id.yblx_QT);
        // 是否户主
        sfhz_hj = (RadioButton) findViewById(R.id.sfhz_hj);
        sfhz_fhj = (RadioButton) findViewById(R.id.sfhz_fhj);
        // 遗传病史
        ycbs01 = (RadioGroup) view1.findViewById(R.id.ycbs01);
        ycbs_N = (RadioButton) view1.findViewById(R.id.ycbs_N);
        ycbs_Y = (RadioButton) view1.findViewById(R.id.ycbs_Y);
        // 残疾情况
        cjqk01 = (RadioGroup) view1.findViewById(R.id.cjqk01);
        cjqk_N = (CheckBox) view1.findViewById(R.id.cjqk_N);
        cjqk_SLCJ = (CheckBox) view1.findViewById(R.id.cjqk_SLCJ);
        cjqk_TLCJ = (CheckBox) view1.findViewById(R.id.cjqk_TLCJ);
        cjqk_YYCJ = (CheckBox) view1.findViewById(R.id.cjqk_YYCJ);
        cjqk_ZTCJ = (CheckBox) view1.findViewById(R.id.cjqk_ZTCJ);
        cjqk_ZLCJ = (CheckBox) view1.findViewById(R.id.cjqk_ZLCJ);
        cjqk_JSCJ = (CheckBox) view1.findViewById(R.id.cjqk_JSCJ);
        cjqk_QTCJ = (CheckBox) view1.findViewById(R.id.cjqk_QTCJ);
        cjqk_et = (EditText) view1.findViewById(R.id.cjqk_et);
        qtzf = (EditText) view1.findViewById(R.id.qtzf);
        ycbs02 = (EditText) view1.findViewById(R.id.ycbs02);

    }

    /**
     * 个人信息
     *
     * @param view
     */
    private void InitPersonInfoView(Activity view) {
        grxm01 = (EditText) view.findViewById(R.id.grxm01);
        wgdz = (AutoCompleteTextView) view.findViewById(R.id.wgzz);
//        ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this,
//                android.R.layout.simple_list_item_1,
//                JKDAContantsTest.WGDZ_QIANDONG.testQIANDONG);
//        wgdz.setAdapter(arrayAdapter);
        dabh01 = (EditText) view.findViewById(R.id.dabh01);
        grxb01 = (RadioGroup) view.findViewById(R.id.grxb01);
        gexi_xb01 = (RadioButton) view.findViewById(R.id.gexi_xb01);
        gexi_xb02 = (RadioButton) view.findViewById(R.id.gexi_xb02);
        csrq01 = (EditText) view.findViewById(R.id.csrq01);
        sfzh01 = (EditText) view.findViewById(R.id.sfzh01);
        gzdw01 = (EditText) view.findViewById(R.id.gzdw01);
        grmz01 = (SingleChoiceEditText) view.findViewById(R.id.grmz01);
        String[] items = NationUtil.getNationArray();
        grmz01.setFixItems(items);
        grdh01 = (EditText) view.findViewById(R.id.grdh01);
        lxr001 = (EditText) view.findViewById(R.id.lxr001);
        lxdh01 = (EditText) view.findViewById(R.id.lxdh01);
        gxjg = (EditText) view.findViewById(R.id.gxjg);
        zrys = (EditText) view.findViewById(R.id.zrys);
        grbm = (EditText) view.findViewById(R.id.grbm01);
        xzjg01 = (RadioGroup) view.findViewById(R.id.xzjg01);
        xzjg_hj = (RadioButton) view.findViewById(R.id.xzjg_hj);
        xzjg_fhj = (RadioButton) view.findViewById(R.id.xzjg_fhj);
        nyhj_s = (RadioButton) view.findViewById(R.id.nyhj_s);
        nyhj_f = (RadioButton) view.findViewById(R.id.nyhj_f);
        // 从身份证号码获取出生日期。
        sfzh01.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                temp = s;
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                CharSequence sfzh = temp;
                if (sfzh.length() == 18) {
                    CharSequence nian = sfzh.subSequence(6, 10);
                    CharSequence yue = sfzh.subSequence(10, 12);
                    CharSequence ri = sfzh.subSequence(12, 14);
                    csrq01.setText(nian + "-" + yue + "-" + ri);
                    CharSequence sexC = sfzh.subSequence(16, 17);
                    String sex = sexC + "";
                    if ("1".equals(sex) || "3".equals(sex) || "5".equals(sex)
                            || "7".equals(sex) || "9".equals(sex)) {
                        gexi_xb01.setChecked(true);
                    } else if ("0".equals(sex) || "2".equals(sex)
                            || "4".equals(sex) || "6".equals(sex)
                            || "8".equals(sex)) {
                        gexi_xb02.setChecked(true);
                    }
                    queryUserDetailFromWeb(sfzh01.getText().toString().trim());
                } else {
                    csrq01.setText("");
                }
            }
        });
        String digists = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        sfzh01.setKeyListener(DigitsKeyListener.getInstance(digists));
        sfzh01.setTransformationMethod(new AllCapTransformationMethod(true));
    }

    /***
     * 根据身份证从后台获取该人的健康档案信息。
     */
    private void queryUserDetailFromWeb(String idCard) {
        WebService.queryArchiveList(context, idCard, new StringCallback() {

            @Override
            public void onSuccess(String str, Call call, Response response) {
                int middle = str.length() / 2;
                String str1 = str.substring(0, middle);
                String str2 = str.substring(middle, str.length());
                System.out.println("获取的查询健康档案信息1：" + str1);
                System.out.println("获取的查询健康档案信息2：" + str2);
                System.out.println("获取的查询健康档案信息response：" + response);
                JSONObject obj;
                try {
                    obj = new JSONObject(str);
                    JSONArray data = obj.optJSONArray("data");
                    if (data != null && data.length() > 0) {
                        JSONObject userObj = (JSONObject) data.get(0);
                        String empiId = userObj.optString("empiId");
                        // 查询健康档案的信息。
                        getArchiveDetail(empiId);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
            }
        });
    }

    private void getArchiveDetail(String empiId) {
        WebService.getArchiveDetail(context, empiId, new StringCallback() {

            @Override
            public void onSuccess(String str, Call call, Response response) {
                System.out.println("获取单一的ArchiveDetail：" + str);
                JSONObject obj;
                try {
                    obj = new JSONObject(str);
                    String jsonStr = obj.optString("data");
                    if (!TextUtils.isEmpty(jsonStr)) {
                        Data_getArchiveDetail data = new Gson().fromJson(
                                jsonStr, Data_getArchiveDetail.class);
                        GetArchiveDetail getArchiveDetail = data
                                .getArchiveDetail();
                        System.out.println(getArchiveDetail);
                        setQueryData(data);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public class AllCapTransformationMethod extends
            ReplacementTransformationMethod {

        private char[] lower = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
                'v', 'w', 'x', 'y', 'z'};
        private char[] upper = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
                'V', 'W', 'X', 'Y', 'Z'};
        private boolean allUpper = false;

        public AllCapTransformationMethod(boolean needUpper) {
            this.allUpper = needUpper;
        }

        @Override
        protected char[] getOriginal() {
            if (allUpper) {
                return lower;
            } else {
                return upper;
            }
        }

        @Override
        protected char[] getReplacement() {
            if (allUpper) {
                return upper;
            } else {
                return lower;
            }
        }
    }

    private void InitPersonShenghuoView(Activity view4) {
        // 厨房排风设施代码
        cfss01 = (RadioGroup) view4.findViewById(R.id.cfss01);
        cfss01_W = (RadioButton) view4.findViewById(R.id.cfss01_W);
        cfss01_YYJ = (RadioButton) view4.findViewById(R.id.cfss01_YYJ);
        cfss01_HQS = (RadioButton) view4.findViewById(R.id.cfss01_HQS);
        cfss01_YC = (RadioButton) view4.findViewById(R.id.cfss01_YC);
        cfss01_QT = (RadioButton) view4.findViewById(R.id.cfss01_QT);
        // 燃料类型
        rllx01 = (RadioGroup) view4.findViewById(R.id.rllx01);
        rllx01_YHQ = (RadioButton) view4.findViewById(R.id.rllx01_YHQ);
        rllx01_M = (RadioButton) view4.findViewById(R.id.rllx01_M);
        rllx01_TRQ = (RadioButton) view4.findViewById(R.id.rllx01_TRQ);
        rllx01_ZQ = (RadioButton) view4.findViewById(R.id.rllx01_ZQ);
        rllx01_CH = (RadioButton) view4.findViewById(R.id.rllx01_CH);
        rllx01_QT = (RadioButton) view4.findViewById(R.id.rllx01_QT);
        // 饮水
        ysqk01 = (RadioGroup) view4.findViewById(R.id.ysqk01);
        ysqk01_ZLS = (RadioButton) view4.findViewById(R.id.ysqk01_ZLS);
        ysqk01_JJH = (RadioButton) view4.findViewById(R.id.ysqk01_JJH);
        ysqk01_JS = (RadioButton) view4.findViewById(R.id.ysqk01_JS);
        ysqk01_HHS = (RadioButton) view4.findViewById(R.id.ysqk01_HHS);
        ysqk01_TS = (RadioButton) view4.findViewById(R.id.ysqk01_TS);
        ysqk01_QT = (RadioButton) view4.findViewById(R.id.ysqk01_QT);
        // 厕所
        csqk01 = (RadioGroup) view4.findViewById(R.id.csqk01);
        csqk01_WSCS = (RadioButton) view4.findViewById(R.id.csqk01_WSCS);
        csqk01_FCS = (RadioButton) view4.findViewById(R.id.csqk01_FCS);
        csqk01_MT = (RadioButton) view4.findViewById(R.id.csqk01_MT);
        csqk01_FK = (RadioButton) view4.findViewById(R.id.csqk01_FK);
        csqk01_PC = (RadioButton) view4.findViewById(R.id.csqk01_PC);
        csqk01_QT = (RadioButton) view4.findViewById(R.id.csqk01_QT);
        // 禽畜栏
        qcqk01 = (RadioGroup) view4.findViewById(R.id.qcqk01);
        qcqk01_DS = (RadioButton) view4.findViewById(R.id.qcqk01_DS);
        qcqk01_SN = (RadioButton) view4.findViewById(R.id.qcqk01_SN);
        qcqk01_SW = (RadioButton) view4.findViewById(R.id.qcqk01_SW);
        qcqk01_W = (RadioButton) view4.findViewById(R.id.qcqk01_W);
        // 建档方式
        jdfs01 = (RadioGroup) view4.findViewById(R.id.jdfs01);
        jdfs01_ZL = (RadioButton) view4.findViewById(R.id.jdfs01_ZL);
        jdfs01_GW = (RadioButton) view4.findViewById(R.id.jdfs01_GW);
        jdfs01_YTJ = (RadioButton) view4.findViewById(R.id.jdfs01_YTJ);
        jdfs01_QT = (RadioButton) view4.findViewById(R.id.jdfs01_QT);
        czsj01 = (EditText) view4.findViewById(R.id.czsj01);
        jjly_SHJJ = (RadioButton) view4.findViewById(R.id.jjly_SHJJ);
        jjly_GZSR = (RadioButton) view4.findViewById(R.id.jjly_GZSR);
        jjly_QT = (RadioButton) view4.findViewById(R.id.jjly_QT);
    }

    /***
     * 检查编辑框。是否可以编辑的。
     *
     * @return
     */
    public boolean isEditTextOK() {
        if (TextUtils.isEmpty(grxm01.getText().toString())) {
            handler.obtainMessage(WHAT_NAMENULL).sendToTarget();
            return false;
        } else if (TextUtils.isEmpty(sfzh01.getText().toString())) {
            handler.obtainMessage(WHAT_CARDNONULL).sendToTarget();
            return false;
        } else if (TextUtils.isEmpty(csrq01.getText().toString())) {
            handler.obtainMessage(WHAT_BIRTHDAYNULL).sendToTarget();
            return false;
        }
		else if (TextUtils.isEmpty(grdh01.getText().toString())) {
			handler.obtainMessage(WHAT_PHONENULL).sendToTarget();
			return false;
		}
		else if (TextUtils.isEmpty(wgdz.getText().toString())) {
			handler.obtainMessage(WHAT_ADDRESSNULL).sendToTarget();
			return false;
		}
        else if (!IdCardCheckTool.checkIdCard(sfzh01.getText().toString()
                .trim())) {
            handler.obtainMessage(WHAT_CARDNOERROR).sendToTarget();
            return false;
        }
        return true;
    }

    private static final int WHAT_NAMENULL = 23;
    private static final int WHAT_CARDNONULL = 33;
    private static final int WHAT_BIRTHDAYNULL = 43;
    private static final int WHAT_CARDNOERROR = 53;
    private static final int WHAT_PHONENULL = 63;
    private static final int WHAT_ADDRESSNULL = 73;

    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case WHAT_NAMENULL:
                    ToastTool.showShort(context, "姓名不能为空！");
                    break;
                case WHAT_CARDNONULL:
                    ToastTool.showShort(context, "身份证号码不能为空!");
                    break;
                case WHAT_BIRTHDAYNULL:
                    ToastTool.showShort(context, "出生日期不能为空!");
                    break;
                case WHAT_CARDNOERROR:
                    ToastTool.showShort(context, "身份证号码不合法，请检查后再填写!");
                    break;
                case WHAT_PHONENULL:
                    ToastTool.showShort(context, "电话号码不能为空!");
                    break;
                case WHAT_ADDRESSNULL:
                    ToastTool.showShort(context, "现住址不能为空!");
                    break;
            }
            super.handleMessage(msg);
        }

    };

    private Archive getJsonUpload() {
        Archive archive = new Archive();
        archive.setEmpiId(getTextByEt(grbm));
        archive.setPhrId(getTextByEt(dabh01));
        archive.setIdCard(getTextByEt(sfzh01));
        archive.setPersonName(getTextByEt(grxm01));
        String sexCode = "1";
        // 性别
        if (gexi_xb01.isChecked()) {
            sexCode = "1";
        } else if (gexi_xb02.isChecked()) {
            sexCode = "2";
        } else {

        }
        archive.setSexCode(sexCode);
        archive.setBirthday(getTextByEt(csrq01));
        archive.setMobileNumber(getTextByEt(grdh01));
        // 填写网格地址，需要从服务器上获取
        archive.setRegionCode(JKDAContantsTest.WGDZ_QIANDONG
                .getCodeByAddress(getTextByEt(wgdz)));
        // 网格地址的code值。
        if (!TextUtils.isEmpty(mQueryAddressCode)) {
            archive.setRegionCode(mQueryAddressCode);
        }
        archive.setRegionCode_text(getTextByEt(wgdz));
        // archive.setRegionCode_text("高新区唐家湾镇唐乐社区龙岗北商业城1栋1单元603");
        ArchiveDetail archiveDetailVO = new ArchiveDetail();
        archiveDetailVO.setContact(getTextByEt(lxr001));
        archiveDetailVO.setContactPhone(getTextByEt(lxdh01));
        // 户籍
        if (xzjg_hj.isChecked()) {
            archiveDetailVO.setRegisteredPermanent("1");
        } else if (xzjg_fhj.isChecked()) {
            archiveDetailVO.setRegisteredPermanent("2");
        }
        // 这里还需要填写民族码。
        archiveDetailVO.setNationCode(NationUtil
                .getNationCode(getTextByEt(grmz01)));
        // 血型
        String xuexing = "";
        if (grxx_A.isChecked()) {
            xuexing = 1 + "";
        }
        if (grxx_B.isChecked()) {
            xuexing = 2 + "";
        }
        if (grxx_O.isChecked()) {
            xuexing = 3 + "";
        }
        if (grxx_AB.isChecked()) {
            xuexing = 4 + "";
        }
        if (grxx_BX.isChecked()) {
            xuexing = 5 + "";
        }
        archiveDetailVO.setBloodTypeCode(xuexing + "");
        /**
         * RH阴性
         */
        String rh = "";
        if (rhyx_N.isChecked()) {
            rh = 1 + "";
        } else if (rhyx_Y.isChecked()) {
            rh = 2 + "";
        } else if (rhyx_BX.isChecked()) {
            rh = 3 + "";
        }
        archiveDetailVO.setRhBloodCode(rh + "");
        // 文化程度
        String whcd = "";
        if (whcd_YJS.isChecked()) {
            whcd = 11 + "";
        } else if (whcd_DXBK.isChecked()) {
            whcd = 21 + "";
        } else if (whcd_DX.isChecked()) {
            whcd = 31 + "";
        } else if (whcd_ZZ.isChecked()) {
            whcd = 41 + "";
        } else if (whcd_JGXX.isChecked()) {
            whcd = 50 + "";
        } else if (whcd_GZ.isChecked()) {
            whcd = 61 + "";
        } else if (whcd_CZ.isChecked()) {
            whcd = 71 + "";
        } else if (whcd_XX.isChecked()) {
            whcd = 81 + "";
        } else if (whcd_WM.isChecked()) {
            whcd = 90 + "";
        } else if (whcd_BX.isChecked()) {
            whcd = 01 + "";
        }
        archiveDetailVO.setEducationCode(whcd + "");
        // 婚姻状况
        String hyzk = "";
        if (hyzk_WH.isChecked()) {
            hyzk = 10 + "";
        } else if (hyzk_YH.isChecked()) {
            hyzk = 20 + "";
        } else if (hyzk_CH.isChecked()) {
            hyzk = 21 + "";
        } else if (hyzk_ZH.isChecked()) {
            hyzk = 22 + "";
        } else if (hyzk_FH.isChecked()) {
            hyzk = 23 + "";
        } else if (hyzk_SO.isChecked()) {
            hyzk = 30 + "";
        } else if (hyzk_LH.isChecked()) {
            hyzk = 40 + "";
        } else if (hyzk_QT.isChecked()) {
            hyzk = 50 + "";
        }
        archiveDetailVO.setMaritalStatusCode(hyzk + "");
        // 医疗付费方式
        String zffs = "";
        if (yblx_ZG.isChecked()) {
            zffs = "1";
        } else if (yblx_JM.isChecked()) {
            zffs = "2";
        } else if (yblx_LC.isChecked()) {
            zffs = "3";
        } else if (yblx_PK.isChecked()) {
            zffs = "4";
        } else if (yblx_SY.isChecked()) {
            zffs = "5";
        } else if (yblx_GF.isChecked()) {
            zffs = "6";
        } else if (yblx_ZF.isChecked()) {
            zffs = "7";
        } else if (yblx_QT.isChecked()) {
            zffs = "8";
        }
        archiveDetailVO.setInsuranceCode(zffs);
        // 是否户主
        String sfhz = "";
        if (sfhz_hj.isChecked()) {
            sfhz = "y";
        } else if (sfhz_fhj.isChecked()) {
            sfhz = "n";
        }
        archiveDetailVO.setMasterFlag(sfhz);
        // 农业户籍
        String nyhj = "";
        if (nyhj_s.isChecked()) {
            nyhj = "y";
        } else if (nyhj_f.isChecked()) {
            nyhj = "n";
        }
        archiveDetailVO.setIsAgrRegister(nyhj);
        // 经济来源
        String jjly = "";
        if (jjly_SHJJ.isChecked()) {
            jjly = "1";
        } else if (jjly_GZSR.isChecked()) {
            jjly = "2";
        } else if (jjly_QT.isChecked()) {
            jjly = "3";
        }
        archiveDetailVO.setIncomeSource(jjly);
        // 职业
        String zhiye = "Z";
        if (grzy_JG.isChecked()) {
            zhiye = "0";
        } else if (grzy_JS.isChecked()) {
            zhiye = "1/2";
        } else if (grzy_BS.isChecked()) {
            zhiye = "3";
        } else if (grzy_SY.isChecked()) {
            zhiye = "4";
        } else if (grzy_LL.isChecked()) {
            zhiye = "5";
        } else if (grzy_SC.isChecked()) {
            zhiye = "9-9";
        } else if (grzy_JR.isChecked()) {
            zhiye = "X";
        } else if (grzy_BB.isChecked()) {
            zhiye = "Y";
        } else if (grzy_WZY.isChecked()) {
            zhiye = "Z";
        }
        archiveDetailVO.setWorkCode(zhiye);
        // 厨房排风设施代码
        String chufang = "";
        if (cfss01_W.isChecked()) {
            chufang = "1";
        } else if (cfss01_YYJ.isChecked()) {
            chufang = "2";
        } else if (cfss01_HQS.isChecked()) {
            chufang = "3";
        } else if (cfss01_YC.isChecked()) {
            chufang = "4";
        } else if (cfss01_QT.isChecked()) {
            chufang = "5";
        }
        archiveDetailVO.setCookAirTool(chufang);
        // 燃料类型
        String ranliao = "";
        if (rllx01_YHQ.isChecked()) {
            ranliao = "1";
        } else if (rllx01_M.isChecked()) {
            ranliao = "2";
        } else if (rllx01_TRQ.isChecked()) {
            ranliao = "3";
        } else if (rllx01_ZQ.isChecked()) {
            ranliao = "4";
        } else if (rllx01_CH.isChecked()) {
            ranliao = "5";
        } else if (rllx01_QT.isChecked()) {
            ranliao = "9";
        }
        archiveDetailVO.setFuelType(ranliao);

        // 饮水
        String yinshui = "";
        if (ysqk01_ZLS.isChecked()) {
            yinshui = "1";
        } else if (ysqk01_JJH.isChecked()) {
            yinshui = "2";
        } else if (ysqk01_JS.isChecked()) {
            yinshui = "3";
        } else if (ysqk01_HHS.isChecked()) {
            yinshui = "4";
        } else if (ysqk01_TS.isChecked()) {
            yinshui = "5";
        } else if (ysqk01_QT.isChecked()) {
            yinshui = "9";
        }
        archiveDetailVO.setWaterSourceCode(yinshui);
        // // 厕所
        String cesuo = "";
        if (csqk01_WSCS.isChecked()) {
            cesuo = "1";
        } else if (csqk01_FCS.isChecked()) {
            cesuo = "2";
        } else if (csqk01_MT.isChecked()) {
            cesuo = "3";
        } else if (csqk01_FK.isChecked()) {
            cesuo = "4";
        } else if (csqk01_PC.isChecked()) {
            cesuo = "5";
        } else if (csqk01_QT.isChecked()) {
            cesuo = "9";
        }
        archiveDetailVO.setWashroom(cesuo);

        // // 禽畜栏
        String qinchulan = "";
        if (qcqk01_DS.isChecked()) {
            qinchulan = "1";
        } else if (qcqk01_SN.isChecked()) {
            qinchulan = "2";
        } else if (qcqk01_SW.isChecked()) {
            qinchulan = "3";
        } else if (qcqk01_W.isChecked()) {
            qinchulan = "4";
        }
        archiveDetailVO.setLivestockColumn(qinchulan);
        // archiveDetailVO.setManaUnitId(getTextByEt(gxjg));
        // archiveDetailVO.setManaDoctorId(getTextByEt(zrys));
        archiveDetailVO.setManaUnitId(MyApplication.getOngovId());
        archiveDetailVO.setManaDoctorId(MyApplication.getOnDoctorId());
        archive.setArchiveDetail(archiveDetailVO);
        return archive;
    }

    public void upload() {
        if (!isEditTextOK()) {
            return;
        }
        Archive archive = getJsonUpload();
        archive.setPastHistorylist(jwsList);
        String json = new Gson().toJson(archive);
        JSONDBService.getInstance(context);
        ArchiveBean bean = new ArchiveBean();
        bean.setTime(this.time);
        bean.setIdcard(archive.getIdCard());
        bean.setSex(archive.getSexCode() == "1" ? "男"
                : archive.getSexCode() == "2" ? "女" : "");
        bean.setJson(json);
        bean.setBirthday(archive.getBirthday());
        bean.setName(archive.getPersonName());
        bean.setPhrId(archive.getPhrId());
        JSONDBService.add(bean);
        // 如果是新建档案。就从这里进来。
        // if ("addUser".equals(flag)) {
        BaseActivity.setArchiveBean(bean);
        WebService.addArchive(context, time, jsonHandler, archive);
        // }
        // else {
        // WebService.updateArchive(context, time, jsonHandler, archive);
        // }

    }

    private static final int what_success_ok = 65;
    private static final int what_failed = 77;
    private static final int what_error = 87;
    Handler jsonHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case what_success_ok:
                    ToastTool.showLong(context, "上传成功");
                    break;
                case what_failed:
                    ToastTool.showLong(context, "" + msg.obj);
                    break;
                case what_error:
                    ToastTool.showLong(context, "访问出错，请检查网络或配置信息");
                    break;
            }
        }

        ;
    };

    private void fillTable() {
        // 如果是新建档案。就从这里进来。
        if ("addUser".equals(flag)) {
            String idCard = getIntent().getStringExtra("idCard");
            String name = getIntent().getStringExtra("name");
            String sex = getIntent().getStringExtra("sex");
            if (!TextUtils.isEmpty(idCard) && !TextUtils.isEmpty(name)) {
                sfzh01.setText(idCard.trim().toUpperCase());
                if (idCard.length() == 18) {
                    String birthday = idCard.substring(6, 10) + "-"
                            + idCard.substring(10, 12) + "-"
                            + idCard.substring(12, 14);
                    csrq01.setText(birthday + "");
                }
                grxm01.setText(name.trim());
                if (!TextUtils.isEmpty(sex)) {
                    if (sex.contains("男")) {
                        gexi_xb01.setChecked(true);
                    } else if (sex.contains("女")) {
                        gexi_xb02.setChecked(true);
                    }
                }
                String address = getIntent().getStringExtra("address");
                if (!TextUtils.isEmpty(address)) {
                    wgdz.setText(address.trim());
                }
                String nation = getIntent().getStringExtra("nation");
                if (!TextUtils.isEmpty(nation)) {
                    grmz01.setText(nation + "族");
                }
            }
            return;
        }
        // 从数据库获取健康档案数据。
        Archive archive = new Archive();
        ArchiveBean queryBean = new ArchiveBean();
        queryBean.setTime(time);
        ArchiveBean bean = BaseActivity.getArchiveBean();
        if (!TextUtils.isEmpty(time)) {
            List<ArchiveBean> lists = (List<ArchiveBean>) JSONDBService
                    .queryByKey(queryBean);
            if (lists.size() > 0) {
                bean = lists.get(0);
            }
        }
        if (TextUtils.isEmpty(bean.getJson())) {
            return;
        }
        archive = new Gson().fromJson(bean.getJson(), Archive.class);
        if (archive == null) {
            return;
        }
        jwsList = archive.getPastHistorylist();
        jwsListStr = JibingContants.getArrayByCode(jwsList);
        adapter = new jwsColmunAdapter(context, jwsListStr);
        jws_lv_jws.setAdapter(adapter);
        // setListViewHeight(jws_lv_jws);
        grbm.setText(archive.getEmpiId());// 个人编码
        dabh01.setText(archive.getPhrId());// 档案编号
        sfzh01.setText(archive.getIdCard());// 身份号
        grxm01.setText(archive.getPersonName());// 姓名
        String sexCode = archive.getSexCode();
        if ("1".equals(sexCode)) {
            gexi_xb01.setChecked(true);
        } else if ("2".equals(sexCode)) {
            gexi_xb02.setChecked(true);
        }
        csrq01.setText(archive.getBirthday());
        grdh01.setText(archive.getMobileNumber());// 个人电话
        // 网格地址。网格地址码
        wgdz.setText(archive.getRegionCode_text());// 地址
        ArchiveDetail archiveDetail = archive.getArchiveDetail();
        lxr001.setText(archiveDetail.getContact());// 联系人姓名
        lxdh01.setText(archiveDetail.getContactPhone());// 联系人电话
        String permanentType = archiveDetail.getRegisteredPermanent();
        if ("1".equals(permanentType)) {// 户籍
            xzjg_hj.setChecked(true);
        } else if ("2".equals(permanentType)) {
            xzjg_fhj.setChecked(true);
        }
        // 民族
        String minzu = NationUtil.getNationStr(archiveDetail.getNationCode());
        grmz01.setText(minzu);
        // 血型。
        String xuexing = archiveDetail.getBloodTypeCode();
        if ("1".equals(xuexing)) {
            grxx_A.setChecked(true);
        } else if ("2".equals(xuexing)) {
            grxx_B.setChecked(true);
        } else if ("3".equals(xuexing)) {
            grxx_O.setChecked(true);
        } else if ("4".equals(xuexing)) {
            grxx_AB.setChecked(true);
        } else if ("5".equals(xuexing)) {
            grxx_BX.setChecked(true);
        }
        // rh血型
        String rh = archiveDetail.getRhBloodCode();
        if ("1".equals(rh)) {
            rhyx_N.setChecked(true);
        } else if ("2".equals(rh)) {
            rhyx_Y.setChecked(true);
        } else if ("3".equals(rh)) {
            rhyx_BX.setChecked(true);
        }
        // 文化程度
        String whcd = archiveDetail.getEducationCode();
        switch (whcd) {
            case 11 + "":
                whcd_YJS.setChecked(true);
                break;
            case 21 + "":
                whcd_DXBK.setChecked(true);
                break;
            case 31 + "":
                whcd_DX.setChecked(true);
                break;
            case 41 + "":
                whcd_ZZ.setChecked(true);
                break;
            case 50 + "":
                whcd_JGXX.setChecked(true);
                break;
            case 61 + "":
                whcd_GZ.setChecked(true);
                break;
            case 71 + "":
                whcd_CZ.setChecked(true);
                break;
            case 81 + "":
                whcd_XX.setChecked(true);
                break;
            case 90 + "":
                whcd_WM.setChecked(true);
                break;
            case 01 + "":
                whcd_BX.setChecked(true);
                break;
        }

        // 婚姻状况
        String hyzk = archiveDetail.getMaritalStatusCode();
        switch (hyzk) {
            case 10 + "":
                hyzk_WH.setChecked(true);
                break;
            case 20 + "":
                hyzk_YH.setChecked(true);
                break;
            case 21 + "":
                hyzk_CH.setChecked(true);
                break;
            case 22 + "":
                hyzk_ZH.setChecked(true);
                break;
            case 23 + "":
                hyzk_FH.setChecked(true);
                break;
            case 30 + "":
                hyzk_SO.setChecked(true);
                break;
            case 40 + "":
                hyzk_LH.setChecked(true);
                break;
            case 50 + "":
                hyzk_QT.setChecked(true);
                break;
        }
        // 医疗付费方式
        String zffs = archiveDetail.getInsuranceCode();
        switch (zffs) {
            case "1":
                yblx_ZG.setChecked(true);
                break;
            case "2":
                yblx_JM.setChecked(true);
                break;
            case "3":
                yblx_LC.setChecked(true);
                break;
            case "4":
                yblx_PK.setChecked(true);
                break;
            case "5":
                yblx_SY.setChecked(true);
                break;
            case "6":
                yblx_GF.setChecked(true);
                break;
            case "7":
                yblx_ZF.setChecked(true);
                break;
            case "8":
                yblx_QT.setChecked(true);
                break;
        }
        // 是否户主
        String sfhz = archiveDetail.getMasterFlag();
        if ("y".equals(sfhz)) {
            sfhz_hj.setChecked(true);
        } else if ("n".equals(sfhz)) {
            sfhz_fhj.setChecked(true);
        }
        // 是否农业户籍
        String sfnyhz = archiveDetail.getIsAgrRegister();
        if ("y".equals(sfnyhz)) {
            nyhj_s.setChecked(true);
        } else if ("n".equals(sfnyhz)) {
            nyhj_f.setChecked(true);
        }
        // 经济来源
        String jjly = archiveDetail.getIncomeSource();
        if ("1".equals(jjly)) {
            jjly_SHJJ.setChecked(true);
        } else if ("2".equals(jjly)) {
            jjly_GZSR.setChecked(true);
        } else if ("3".equals(jjly)) {
            jjly_QT.setChecked(true);
        }

        String grzy = archiveDetail.getWorkCode();
        switch (grzy) {
            case "0":
                grzy_JG.setChecked(true);
                break;
            case "1/2":
                grzy_JS.setChecked(true);
                break;
            case "3":
                grzy_BS.setChecked(true);
                break;
            case "4":
                grzy_SY.setChecked(true);
                break;
            case "5":
                grzy_LL.setChecked(true);
                break;
            case "9-9":
                grzy_SC.setChecked(true);
                break;
            case "X":
                grzy_JR.setChecked(true);
                break;
            case "Y":
                grzy_BB.setChecked(true);
                break;
            case "Z":
                grzy_WZY.setChecked(true);
                break;
        }
        // 厨房
        String chufang = archiveDetail.getCookAirTool();
        if ("1".equals(chufang)) {
            cfss01_W.setChecked(true);
        } else if ("2".equals(chufang)) {
            cfss01_YYJ.setChecked(true);
        } else if ("3".equals(chufang)) {
            cfss01_HQS.setChecked(true);
        } else if ("4".equals(chufang)) {
            cfss01_YC.setChecked(true);
        } else if ("9".equals(chufang)) {
            cfss01_QT.setChecked(true);
        }
        // 燃料类型
        String ranliao = archiveDetail.getFuelType();
        switch (ranliao) {
            case "1":
                rllx01_YHQ.setChecked(true);
                break;
            case "2":
                rllx01_M.setChecked(true);
                break;
            case "3":
                rllx01_TRQ.setChecked(true);
                break;
            case "4":
                rllx01_ZQ.setChecked(true);
                break;
            case "5":
                rllx01_CH.setChecked(true);
                break;
            case "9":
                rllx01_QT.setChecked(true);
                break;
        }

        // 饮水
        String yinshui = archiveDetail.getWaterSourceCode();
        switch (yinshui) {
            case "1":
                ysqk01_ZLS.setChecked(true);
                break;
            case "2":
                ysqk01_JJH.setChecked(true);
                break;
            case "3":
                ysqk01_JS.setChecked(true);
                break;
            case "4":
                ysqk01_HHS.setChecked(true);
                break;
            case "5":
                ysqk01_TS.setChecked(true);
                break;
            case "9":
                ysqk01_QT.setChecked(true);
                break;
        }
        // 厕所
        String cesuo = archiveDetail.getWashroom();
        switch (cesuo) {
            case "1":
                csqk01_WSCS.setChecked(true);
                break;
            case "2":
                csqk01_FCS.setChecked(true);
                break;
            case "3":
                csqk01_MT.setChecked(true);
                break;
            case "4":
                csqk01_FK.setChecked(true);
                break;
            case "5":
                csqk01_PC.setChecked(true);
                break;
            case "9":
                csqk01_QT.setChecked(true);
                break;
        }
        // 禽畜栏
        String qinchulan = archiveDetail.getLivestockColumn();
        switch (qinchulan) {
            case "1":
                qcqk01_DS.setChecked(true);
                break;
            case "2":
                qcqk01_SN.setChecked(true);
                break;
            case "3":
                qcqk01_SW.setChecked(true);
                break;
            case "4":
                qcqk01_W.setChecked(true);
                break;
        }

        String zrysId = archiveDetail.getManaDoctorId();
        zrys.setText(zrysId);
        String gxjgId = archiveDetail.getManaUnitId();
        gxjg.setText(gxjgId);
    }

    /***
     * 设置查询下来的健康档案信息。就不用手动建档了。
     */

    private List<PastHistorylist> getListFromWeb(List<pasthistory> list) {
        List<PastHistorylist> result = new ArrayList<PastHistorylist>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                pasthistory temp = list.get(i);
                PastHistorylist listTemp = new PastHistorylist();
                listTemp.setEmpiId(temp.getEmpiId());
                listTemp.setPastHisTypeCode(temp.getPastHisTypeCode());
                listTemp.setMethodsCode(temp.getMethodsCode());
                listTemp.setProtect(temp.getProtect());
                listTemp.setDiseaseCode(temp.getDiseaseCode());
                listTemp.setDiseaseText(temp.getDiseaseText());
                listTemp.setVestingCode(temp.getVestingCode());
                listTemp.setStartDate(temp.getStartDate());
                listTemp.setEndDate(temp.getEndDate());
                listTemp.setConfirmDate(temp.getConfirmDate());
                listTemp.setRecordUnit(temp.getRecordUnit());
                listTemp.setRecordUser(temp.getRecordUser());
                listTemp.setRecordDate(temp.getRecordDate());
                listTemp.setLastModifyUser(temp.getLastModifyUser());
                listTemp.setLastModifyUnit(temp.getLastModifyUnit());
                listTemp.setLastModifyDate(temp.getLastModifyDate());
                result.add(listTemp);
            }
        }
        return result;
    }

    String mQueryAddress = "";
    String mQueryAddressCode = "";
    GetArchiveDetail mGetArchiveDetail;

    private void setQueryData(Data_getArchiveDetail data) {
        if (data == null) {
            return;
        }
        GetArchiveDetail archive = data.getArchiveDetail();
        mGetArchiveDetail = archive;
        jwsList = getListFromWeb(data.getPastHistory());
        jwsListStr = JibingContants.getArrayByCode(jwsList);
        adapter = new jwsColmunAdapter(context, jwsListStr);
        jws_lv_jws.setAdapter(adapter);
        // setListViewHeight(jws_lv_jws);
        grbm.setText(archive.getEmpiId());// 个人编码
        dabh01.setText(archive.getPhrId());// 档案编号
        // sfzh01.setText(archive.getIdCard());// 身份号
        grxm01.setText(archive.getPersonName());// 姓名
        String sexCode = archive.getSexCode();
        mQueryAddressCode = archive.getRegionCode();
        mQueryAddress = archive.getAddress();
        if ("1".equals(sexCode)) {
            gexi_xb01.setChecked(true);
        } else if ("2".equals(sexCode)) {
            gexi_xb02.setChecked(true);
        }
        csrq01.setText(archive.getBirthday());
        grdh01.setText(archive.getMobileNumber());// 个人电话
        // 网格地址。网格地址码
        wgdz.setText(archive.getAddress());// 地址
        GetArchiveDetail archiveDetail = archive;
        lxr001.setText(archiveDetail.getContact());// 联系人姓名
        lxdh01.setText(archiveDetail.getContactPhone());// 联系人电话
        String permanentType = archiveDetail.getRegisteredPermanent();
        if ("1".equals(permanentType)) {// 户籍
            xzjg_hj.setChecked(true);
        } else if ("2".equals(permanentType)) {
            xzjg_fhj.setChecked(true);
        }
        // 民族
        String minzu = NationUtil.getNationStr(archiveDetail.getNationCode());
        grmz01.setText(minzu);
        // 血型。
        String xuexing = archiveDetail.getBloodTypeCode();
        if (xuexing == null) {
            xuexing = "";
        }
        if ("1".equals(xuexing)) {
            grxx_A.setChecked(true);
        } else if ("2".equals(xuexing)) {
            grxx_B.setChecked(true);
        } else if ("3".equals(xuexing)) {
            grxx_O.setChecked(true);
        } else if ("4".equals(xuexing)) {
            grxx_AB.setChecked(true);
        } else if ("5".equals(xuexing)) {
            grxx_BX.setChecked(true);
        }
        // rh血型
        String rh = archiveDetail.getRhBloodCode();
        if ("1".equals(rh)) {
            rhyx_N.setChecked(true);
        } else if ("2".equals(rh)) {
            rhyx_Y.setChecked(true);
        } else if ("3".equals(rh)) {
            rhyx_BX.setChecked(true);
        }
        // 文化程度
        String whcd = archiveDetail.getEducationCode();
        if (whcd == null) {
            whcd = "";
        }
        switch (whcd) {
            case 11 + "":
                whcd_YJS.setChecked(true);
                break;
            case 21 + "":
                whcd_DXBK.setChecked(true);
                break;
            case 31 + "":
                whcd_DX.setChecked(true);
                break;
            case 41 + "":
                whcd_ZZ.setChecked(true);
                break;
            case 50 + "":
                whcd_JGXX.setChecked(true);
                break;
            case 61 + "":
                whcd_GZ.setChecked(true);
                break;
            case 71 + "":
                whcd_CZ.setChecked(true);
                break;
            case 81 + "":
                whcd_XX.setChecked(true);
                break;
            case 90 + "":
                whcd_WM.setChecked(true);
                break;
            case 01 + "":
                whcd_BX.setChecked(true);
                break;
        }

        // 婚姻状况
        String hyzk = archiveDetail.getMaritalStatusCode();
        if (hyzk == null) {
            hyzk = "";
        }
        switch (hyzk) {
            case 10 + "":
                hyzk_WH.setChecked(true);
                break;
            case 20 + "":
                hyzk_YH.setChecked(true);
                break;
            case 21 + "":
                hyzk_CH.setChecked(true);
                break;
            case 22 + "":
                hyzk_ZH.setChecked(true);
                break;
            case 23 + "":
                hyzk_FH.setChecked(true);
                break;
            case 30 + "":
                hyzk_SO.setChecked(true);
                break;
            case 40 + "":
                hyzk_LH.setChecked(true);
                break;
            case 50 + "":
                hyzk_QT.setChecked(true);
                break;
        }
        // 医疗付费方式
        String zffs = archiveDetail.getInsuranceCode();
        if (zffs == null) {
            zffs = "";
        }
        switch (zffs) {
            case "1":
                yblx_ZG.setChecked(true);
                break;
            case "2":
                yblx_JM.setChecked(true);
                break;
            case "3":
                yblx_LC.setChecked(true);
                break;
            case "4":
                yblx_PK.setChecked(true);
                break;
            case "5":
                yblx_SY.setChecked(true);
                break;
            case "6":
                yblx_GF.setChecked(true);
                break;
            case "7":
                yblx_ZF.setChecked(true);
                break;
            case "8":
                yblx_QT.setChecked(true);
                break;
        }
        // 是否户主
        String sfhz = archiveDetail.getMasterFlag();
        if ("y".equals(sfhz)) {
            sfhz_hj.setChecked(true);
        } else if ("n".equals(sfhz)) {
            sfhz_fhj.setChecked(true);
        }
        // 是否农业户籍
        String sfnyhz = archiveDetail.getIsAgrRegister();
        if ("y".equals(sfnyhz)) {
            nyhj_s.setChecked(true);
        } else if ("n".equals(sfnyhz)) {
            nyhj_f.setChecked(true);
        }
        // 经济来源
        String jjly = archiveDetail.getIncomeSource();
        if ("1".equals(jjly)) {
            jjly_SHJJ.setChecked(true);
        } else if ("2".equals(jjly)) {
            jjly_GZSR.setChecked(true);
        } else if ("3".equals(jjly)) {
            jjly_QT.setChecked(true);
        }

        String grzy = archiveDetail.getWorkCode();
        if (grzy == null) {
            grzy = "";
        }
        switch (grzy) {
            case "0":
                grzy_JG.setChecked(true);
                break;
            case "1/2":
                grzy_JS.setChecked(true);
                break;
            case "3":
                grzy_BS.setChecked(true);
                break;
            case "4":
                grzy_SY.setChecked(true);
                break;
            case "5":
                grzy_LL.setChecked(true);
                break;
            case "9-9":
                grzy_SC.setChecked(true);
                break;
            case "X":
                grzy_JR.setChecked(true);
                break;
            case "Y":
                grzy_BB.setChecked(true);
                break;
            case "Z":
                grzy_WZY.setChecked(true);
                break;
        }
        // 厨房
        String chufang = archiveDetail.getCookAirTool();
        if ("1".equals(chufang)) {
            cfss01_W.setChecked(true);
        } else if ("2".equals(chufang)) {
            cfss01_YYJ.setChecked(true);
        } else if ("3".equals(chufang)) {
            cfss01_HQS.setChecked(true);
        } else if ("4".equals(chufang)) {
            cfss01_YC.setChecked(true);
        } else if ("9".equals(chufang)) {
            cfss01_QT.setChecked(true);
        }
        // 燃料类型
        String ranliao = archiveDetail.getFuelType();
        if (ranliao == null) {
            ranliao = "";
        }
        switch (ranliao) {
            case "1":
                rllx01_YHQ.setChecked(true);
                break;
            case "2":
                rllx01_M.setChecked(true);
                break;
            case "3":
                rllx01_TRQ.setChecked(true);
                break;
            case "4":
                rllx01_ZQ.setChecked(true);
                break;
            case "5":
                rllx01_CH.setChecked(true);
                break;
            case "9":
                rllx01_QT.setChecked(true);
                break;
        }

        // 饮水
        String yinshui = archiveDetail.getWaterSourceCode();
        if (yinshui == null) {
            yinshui = "";
        }
        switch (yinshui) {
            case "1":
                ysqk01_ZLS.setChecked(true);
                break;
            case "2":
                ysqk01_JJH.setChecked(true);
                break;
            case "3":
                ysqk01_JS.setChecked(true);
                break;
            case "4":
                ysqk01_HHS.setChecked(true);
                break;
            case "5":
                ysqk01_TS.setChecked(true);
                break;
            case "9":
                ysqk01_QT.setChecked(true);
                break;
        }
        // 厕所
        String cesuo = archiveDetail.getWashroom();
        if (cesuo == null) {
            cesuo = "";
        }
        switch (cesuo) {
            case "1":
                csqk01_WSCS.setChecked(true);
                break;
            case "2":
                csqk01_FCS.setChecked(true);
                break;
            case "3":
                csqk01_MT.setChecked(true);
                break;
            case "4":
                csqk01_FK.setChecked(true);
                break;
            case "5":
                csqk01_PC.setChecked(true);
                break;
            case "9":
                csqk01_QT.setChecked(true);
                break;
        }
        // 禽畜栏
        String qinchulan = archiveDetail.getLivestockColumn();
        if (qinchulan == null) {
            qinchulan = "";
        }
        switch (qinchulan) {
            case "1":
                qcqk01_DS.setChecked(true);
                break;
            case "2":
                qcqk01_SN.setChecked(true);
                break;
            case "3":
                qcqk01_SW.setChecked(true);
                break;
            case "4":
                qcqk01_W.setChecked(true);
                break;
        }
        String zrysId = archiveDetail.getManaDoctorId();
        zrys.setText(zrysId);
        String gxjgId = archiveDetail.getManaUnitId();
        gxjg.setText(gxjgId);
    }

    private void initPersonInfoView() {
        InitPersonInfoView(this);
        // InitPersonHealView(this);
        // InitPersonHealJiaView(this);
        InitPersonShenghuoView(this);
        InitPersonYibanView(this);
    }

    public void setListViewHeight(ListView listView) {
        // 获取ListView对应的Adapter
        // ListAdapter listAdapter = listView.getAdapter();
        if (adapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = adapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = adapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (adapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    private void findView() {
        jws_lv_jws = (ListView) findViewById(R.id.jws_lv_jws);
        jws_lv_jws.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view,
                                           final int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        JKDAActivity.this);
                builder.setMessage("您确定要删除该离线记录？");
                builder.setPositiveButton("确定",
                        new AlertDialog.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                if (position <= jwsListStr.size()) {
                                    jwsListStr.remove(position);
                                }
                                if (position <= jwsList.size()) {
                                    jwsList.remove(position);
                                }
                                adapter.notifyDataSetChanged();
                            }
                        });
                builder.setNegativeButton("取消", null);
                builder.create().show();
                return true;
            }
        });
        adapter = new jwsColmunAdapter(context, jwsListStr);
        jws_lv_jws.setAdapter(adapter);
        setListViewHeight(jws_lv_jws);
        nameEditText = (EditText) findViewById(R.id.et_name);
        userIdEditText = (EditText) findViewById(R.id.et_userId);
        genderEditText = (EditText) findViewById(R.id.set_gender);
        birthEditText = (EditText) findViewById(R.id.et_birth);
        idNumberEditText = (EditText) findViewById(R.id.et_idNUmber);
        companyEditText = (EditText) findViewById(R.id.et_company);
        selfPhoneEditText = (EditText) findViewById(R.id.et_selfPhone);
        contactNameEditText = (EditText) findViewById(R.id.et_contactName);
        contactPhoneEditText = (EditText) findViewById(R.id.et_contactPhone);
        permanentTypeEditText = (EditText) findViewById(R.id.set_permanentType);
        nationEditText = (EditText) findViewById(R.id.set_nation);
        bloodTypeEditText = (EditText) findViewById(R.id.set_bloodType);
        rHTypeEditText = (EditText) findViewById(R.id.set_RHType);
        maritalStatusEditText = (EditText) findViewById(R.id.set_maritalStatus);
        educationDegreeEditText = (EditText) findViewById(R.id.set_educationDegree);
        jobsEditText = (EditText) findViewById(R.id.set_jobs);
        paymentTypeEditText = (EditText) findViewById(R.id.met_paymentType);
        allergicHistoryEditText = (EditText) findViewById(R.id.met_allergicHistory);
        exposeHistoryEditText = (EditText) findViewById(R.id.met_exposeHistory);
        illnessEditText = (EditText) findViewById(R.id.met_illness);
        operationEditText = (EditText) findViewById(R.id.set_operation);
        traumaEditText = (EditText) findViewById(R.id.set_trauma);
        transfusionEditText = (EditText) findViewById(R.id.set_transfusion);
        fatherHistoryEditText = (EditText) findViewById(R.id.met_fatherHistory);
        motherHistoryEditText = (EditText) findViewById(R.id.met_motherHistory);
        brotherSisterHistoryEditText = (EditText) findViewById(R.id.met_brothersSistersHistory);
        childernHistoryEditText = (EditText) findViewById(R.id.met_childrenHistory);
        genopathyEditText = (EditText) findViewById(R.id.set_genopathy);
        deformityStateEditText = (EditText) findViewById(R.id.set_deformityState);
        airExhaustEditText = (EditText) findViewById(R.id.set_airExhaust);
        fuleTypeEditText = (EditText) findViewById(R.id.set_fuleType);
        waterTypeEditText = (EditText) findViewById(R.id.set_waterType);
        toiletTypeEditText = (EditText) findViewById(R.id.set_toiletType);
        animalHomeEditText = (EditText) findViewById(R.id.set_animalHome);
        btn_save = (Button) findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // dialog.show();
                System.out.println("正在发送");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        upload();
                    }
                }).start();

            }
        });
    }

    private String flag;

}
